<template>
  <div class="wrapper">
    <!-- 卡片组件 -->
    <card :_Size="16" :_Title="$t('my.after_sale')"></card>
    <!-- 搜索 筛選 -->
    <div class="mb_20 box">
      <div class="global_float_right">
        <Input
            v-model="params.keywords"
            class="width_300"
            enter-button
            :placeholder="$t('my.order_search')"
            search
            @on-search="getList"
        />
      </div>
    </div>
    <!-- 列表 -->
    <empty v-if="orderList.length === 0"/>
    <div v-else class="order-content">
      <template v-for="(order, onderIndex) in orderList">
        <div :key="onderIndex" class="order-list">
          <div class="order-header">
            <div>
              <div v-if="order.serviceStatus">{{ filterOrderStatus(order.serviceStatus) }}</div>
              <div>
                售後單號：{{ order.sn }} &nbsp; &nbsp; &nbsp;{{ order.createTime }}
                &nbsp; &nbsp;{{ order.memberName | secrecyMobile }}
              </div>
            </div>
            <div>
              <span>申請退款金额：<span class="global_color">{{
                  order.applyRefundPrice | unitPrice("HK$")
                }}</span></span>
            </div>
          </div>
          <div class="order-body">
            <div class="goods-list">
              <img :src="order.goodsImage" alt="" class="hover-color" @click="goodsDetail(order.skuId, order.goodsId)"/>

              <div>
                <div class="hover-color" @click="goodsDetail(order.skuId, order.goodsId)">{{ order.goodsName }}</div>
                <div class="mt_10"> x {{ order.num }}
                </div>
              </div>
            </div>
            <div>
              <span @click="shopPage(order.shopId)">{{ order.storeName }}</span>
            </div>
            <div>
              <!-- 訂單基础操作 -->
              <Button size="small" type="info" @click="goDetail(order.sn)">售後詳情</Button>
              <Button v-if="order.serviceStatus == 'PASS' &&
                order.serviceType != 'RETURN_MONEY'"
                      size="small"
                      type="warning" @click="openModal(order)">提交物流
              </Button>
              <Button v-if="order.afterSaleAllowOperationVO.cancel" size="small" type="error" @click="cancel(order.sn)">
                取消售後
              </Button>
            </div>
          </div>
        </div>
      </template>
      <Spin v-if="spinShow" fix size="large"></Spin>
    </div>
    <!-- 分頁 -->
    <div class="page-size">
      <Page :page-size="params.pageSize" :total="total"
            show-sizer
            @on-change="changePageNum"
            @on-page-size-change="changePageSize">
      </Page>
    </div>
    <Modal v-model="logisticsShow" width="530">
      <p slot="header">
        <span>提交物流信息</span>
      </p>
      <div>
        <div class="goods-list modal-goods">
          <img :src="singleOrder.goodsImage" alt=""
               class="hover-color" @click="goodsDetail(singleOrder.skuId, singleOrder.goodsId)"/>
          <div>
            <div class="hover-color" @click="goodsDetail(singleOrder.skuId, singleOrder.goodsId)">
              {{ singleOrder.goodsName }}
            </div>
            <div class="mt_10">
              <span class="global_color"
              >{{ singleOrder.flowPrice | unitPrice("HK$") }} </span
              >x {{ singleOrder.num }}
            </div>
          </div>
        </div>
        <Form ref="form" :label-width="100" :model="form" :rules="rules" label-position="left">
          <FormItem label="物流公司" prop="logisticsId">
            <Select v-model="form.logisticsId" placeholder="請選擇物流公司">
              <Option v-for="item in companyList" :key="item.id" :value="item.id">{{ item.name }}</Option>
            </Select>
          </FormItem>
          <FormItem label="物流單號" prop="logisticsNo">
            <Input v-model="form.logisticsNo" placeholder="請填写物流單號"></Input>
          </FormItem>
          <FormItem label="發貨時間" prop="mDeliverTime">
            <DatePicker v-model="form.mDeliverTime" format="yyyy-MM-dd" placeholder="選擇發貨時間" style="width:100%"
                        type="date" @on-change="changeTime"></DatePicker>
          </FormItem>
        </Form>
      </div>
      <div slot="footer" style="text-align: right">
        <Button @click="logisticsShow = false">关闭</Button>
        <Button :loading="submitLoading" type="primary" @click="submitDelivery">提交</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {afterSaleList, cancelAfterSale} from '@/api/member.js';
import {afterSaleDelivery, getLogisticsCompany} from '@/api/order.js';
import {afterSaleStatusList} from '../enumeration.js'
import i18n from "@/lang";

export default {
  i18n,
  name: 'AfterSale',
  data() {
    return {
      orderList: [], // 訂單列表
      params: { // 請求參數
        pageNumber: 1,
        pageSize: 10,
        sn: '',
        sort: 'createTime',
        order: 'desc'
      },
      // 狀態數组
      afterSaleStatusList,
      total: 0, // 訂單总數
      spinShow: false, // 加载狀態
      companyList: [], // 物流公司列表
      logisticsShow: false, // 物流信息modal
      singleOrder: {}, // 單独的售後信息
      form: { // 物流信息數据
        afterSaleSn: '',
        logisticsId: '',
        logisticsNo: '',
        mDeliverTime: ''
      },
      rules: { // 必填校验
        logisticsId: [{required: true, message: '請選擇物流公司'}],
        logisticsNo: [{required: true, message: '請填写物流编號'}],
        mDeliverTime: [{required: true, message: '請選擇發貨時間'}]
      },
      submitLoading: false // 提交加载狀態
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    goDetail(sn) { // 跳轉售後詳情
      this.$router.push({
        name: 'AfterSaleDetail',
        query: {sn}
      })
    },
    cancel(sn) { // 取消售後申請
      this.$Modal.confirm({
        title: '取消',
        content: '<p>确定取消此次售後申請吗？</p>',
        onOk: () => {
          cancelAfterSale(sn).then(res => {
            if (res.success) {
              this.$Message.success('取消售後申請成功')
              this.getList()
            }
          })
        },
        onCancel: () => {
        }
      });
    },
    goodsDetail(skuId, goodsId) {
      // 跳轉商品詳情
      let routeUrl = this.$router.resolve({
        path: '/goodsDetail',
        query: {skuId, goodsId}
      });
      window.open(routeUrl.href, '_blank');
    },
    // 跳轉店铺首頁
    shopPage(id) {
      let routeUrl = this.$router.resolve({
        path: '/Merchant',
        query: {id: id}
      });
      window.open(routeUrl.href, '_blank');
    },
    getList() { // 获取售後列表
      this.spinShow = true;
      let params = JSON.parse(JSON.stringify(this.params))
      afterSaleList(params).then(res => {
        this.spinShow = false
        if (res.success) {
          this.orderList = res.result.records;
          this.total = res.result.total;
        }
      });
    },
    changePageNum(val) { // 修改頁碼
      this.params.pageNumber = val;
      this.getList()
    },
    changePageSize(val) { // 修改頁數
      this.params.pageNumber = 1;
      this.params.pageSize = val;
      this.getList()
    },
    filterOrderStatus(status) { // 获取訂單狀態中文
      const ob = this.afterSaleStatusList.filter(e => {
        return e.status === status
      });
      return ob[0].name
    },
    // 获取物流公司列表
    getCompany() {
      getLogisticsCompany().then(res => {
        if (res.success) {
          this.companyList = res.result
        }
      })
    },
    // 提交物流信息
    submitDelivery() {
      this.submitLoading = true
      afterSaleDelivery(this.form).then(res => {
        if (res.success) {
          this.logisticsShow = false;
          this.$Message.success('提交成功')
          this.getList()
        }
        this.submitLoading = false
      }).catch(() => {
        this.submitLoading = false
      })
    },
    // 提交物流modal
    openModal(row) {
      console.log(row);
      this.singleOrder = row;
      this.form.afterSaleSn = row.sn
      this.logisticsShow = true;
      this.$refs.form.resetFields()
      if (!this.companyList.length) {
        this.getCompany()
      }
    },
    // 格式化時間
    changeTime(time) {
      this.form.mDeliverTime = time;
    }
  }
};
</script>

<style lang="scss" scoped>
.wrapper {
  margin-bottom: 40px;
}

.box {
  overflow: hidden;
}

.page-size {
  margin: 15px 0px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

/** 訂單列表 */
.order-list {
  border: 1px solid #ddd;
  border-radius: 3px;
  margin-bottom: 10px;

  &:hover {
    .del-btn {
      visibility: visible;
    }
  }

  .del-btn {
    visibility: hidden;
  }

  .order-header {
    display: flex;
    align-items: center;
    padding: 10px;
    justify-content: space-between;
    border-bottom: 1px solid #ddd;

    > div:nth-child(1) > div:nth-child(2) {
      font-size: 12px;
      color: #999;
      margin-top: 3px;
    }
  }

  .order-body {
    display: flex;
    justify-content: space-between;
    color: #999;
    padding: 10px;

    > div:nth-child(2) {
      width: 150px;
      text-align: center;

      span {
        color: #438cde;
        cursor: pointer;

        &:hover {
          color: $theme_color;
        }
      }

      .ivu-icon {
        color: #ff8f23;
        cursor: pointer;

        &:hover {
          color: $theme_color;
        }
      }
    }

    > div:nth-child(3) {
      width: 100px;

      .ivu-btn {
        margin-bottom: 10px;
      }
    }
  }
}

.goods-list {
  // width: 500px;
  display: flex;
  margin-bottom: 10px;

  img {
    width: 60px;
    height: 60px;
    margin-right: 10px;
  }

  > div {
    flex: 1;
  }
}

.modal-goods {
  padding: 5px;
}
</style>
